<?php

App::uses('AppController', 'Controller');

class AdminController extends AppController {

    public $components = array(
        'Auth'
    );
    
    public $uses = array('Settings');
    
    public $layout = 'admin';
    
    public $paginate = array(
        'limit' => 10
    );

    public function index() {
        $data = $this->data;
        if (! empty($data)) {
            $saved = true;
            
            $data2 = array();
            foreach ($data['Settings'] as $key=>$value) {
                $sett = $this->Settings->findByKey($key);
                if (! empty($sett)) {
                    $sett['Settings']['value'] = $value;
                } else {
                    $sett = array(
                        'Settings' => array(
                            'id'    => '',
                            'key'   => $key,
                            'value' => $value
                        )
                    );
                }
                
                $data2[] = $sett;
            }
            
            if ($this->Settings->saveAll($data2)) {
                $this->setFlash('Podešavanja su sačuvana.', 'ok');
            } else {
                $this->setFlash('Greška pri čuvanju podataka!');
            }
			
			$this->redirect('index');
        }
        
        $this->set('data', $this->Settings->find('list', array(
            'fields' => array('key', 'value')
        )));
    }

}

